package com.smit.livevideo.sample.fragment;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;

import com.smit.utils.LogUtil;

/**
 * Created by smfang on 2015/11/24.
 * Fragment继承类
 */
public class BaseFragment extends Fragment
{
    private static final String TAG = BaseFragment.class.getSimpleName();
    protected Activity activity;
    private int focusItemId = 0;
    private int containerId = 0;
    private BaseFragment preFragment = null;

    /**
     * fragment按键响应
     * @param keyCode 键值
     * @param event 按键事件
     * @return int
     */
    public int onKeyDown(int keyCode, KeyEvent event)
    {
        return 0;
    }

    public void setPreFragment(BaseFragment f)
    {
        preFragment = f;
    }

    public BaseFragment getPreFragment()
    {
        return preFragment;
    }

    public int getFocusItemId()
    {
        return focusItemId;
    }

    public void setFocusItemId(int id)
    {
        focusItemId = id;
    }

    public int getContainerId()
    {
        return containerId;
    }

    public void setContainerId(int id)
    {
        containerId = id;
    }

    /**
     * 隐藏fragment
     */
    public void hide()
    {
        activity.getFragmentManager().beginTransaction().remove(this).commit();
    }

    @Override
    public void onAttach(Activity activity)
    {
        LogUtil.debug(TAG, "onAttach~~~~~");
        super.onAttach(activity);
        this.activity = activity;
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        LogUtil.debug(TAG, "onCreate~~~~~" + this);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onResume()
    {
        LogUtil.trace(TAG, "onResume~~~~~ setLastFragment " + this);
        FragmentUtil.setLastFragment(this);
        super.onResume();
    }

    @Override
    public void onPause()
    {
        LogUtil.trace(TAG, "onPause~~~~~");
        /* 更新 lastFragment */
        BaseFragment f = FragmentUtil.getLastFragment();
        if (isRemoving())
        {
            if (f == this)
            {
                FragmentUtil.setLastFragment(null);
                LogUtil.trace(TAG, "onPause~~~~~ setLastFragment null");
            }
            if (preFragment != null)
            {
                FragmentUtil.setLastFragment(preFragment);
                LogUtil.trace(TAG, "onPause~~~~~ setLastFragment " + preFragment);
            }
        }
        super.onPause();
    }

    @Override
    public void onDestroy()
    {
        LogUtil.trace(TAG, "onDestroy~~~~~" + this);
        /* hide container */
        activity = getActivity();
        View view;
        if (activity != null)
        {
            view = activity.getWindow().getDecorView();
            if (view != null)
            {
                View childView = view.findViewById(containerId);
                if (childView != null)
                {
                    childView.setVisibility(View.GONE);
                }
            }
        }
        super.onDestroy();
    }
}
